﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FKDatabase;

namespace Test_Operator_Platform.UI
{
    public partial class FrmDebugProduct : Form
    {
        public FrmDebugProduct()
        {
            InitializeComponent();
        }

        private void FrmDebugProduct_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridView1.ReadOnly = false;
            Query();

            this.tool_add.Click += Tool_add_Click;
            this.tool_del.Click += Tool_del_Click;
            this.tool_edit.Click += Tool_edit_Click;
            this.tool_query.Click += Tool_query_Click;
        }

        private void Tool_query_Click(object sender, EventArgs e)
        {
            Query();
        }

        private void Tool_edit_Click(object sender, EventArgs e)
        {
            string sn = "", pn = "", result = "";
            var cRow = this.dataGridView1.CurrentRow;
            if (cRow == null)
            {
                MessageBox.Show("请选择要编辑的行！", "提示");
                return;
            }
            if (cRow.Cells[0].Value != null)
                pn = cRow.Cells[0].Value.ToString();
            if (cRow.Cells[1].Value != null)
                sn = cRow.Cells[1].Value.ToString();
            if (cRow.Cells[2].Value != null)
                result = cRow.Cells[2].Value.ToString();
            if (sn == "")
                return;

            FrmEditDebugPro edit = new FrmEditDebugPro(true, sn, pn, result);
            if (edit.ShowDialog() == DialogResult.OK)
            {
                Query();
            }
        }

        private void Tool_del_Click(object sender, EventArgs e)
        {
            string sn = "", pn = "", result = "";
            var cRow = this.dataGridView1.CurrentRow;
            if (cRow == null)
            {
                MessageBox.Show("请选择要编辑的行！", "提示");
                return;
            }
            if (cRow.Cells[0].Value != null)
                pn = cRow.Cells[0].Value.ToString();
            if (cRow.Cells[1].Value != null)
                sn = cRow.Cells[1].Value.ToString();
            if (cRow.Cells[2].Value != null)
                result = cRow.Cells[2].Value.ToString();
            if (sn == "")
            {
                MessageBox.Show("SerialNumber为空！", "提示");
                return;
            }
            if (Database.DeleteDebugProduct(pn, sn) > 0)
            {
                Query();
            }
            else
            {
                MessageBox.Show("删除失败！", "提示");
            }
        }

        private void Tool_add_Click(object sender, EventArgs e)
        {
            FrmEditDebugPro edit = new FrmEditDebugPro(false, "", "", "");
            if (edit.ShowDialog() == DialogResult.OK)
            {
                Query();
            }
        }

        private void Query()
        {
            var result = Database.QueryDebugProduct("");
            this.dataGridView1.Rows.Clear();
            foreach (var v in result)
            {
                int index = this.dataGridView1.Rows.Add();
                this.dataGridView1.Rows[index].Cells[0].Value = v.PartNumber;
                this.dataGridView1.Rows[index].Cells[1].Value = v.SerialNumber;
                this.dataGridView1.Rows[index].Cells[2].Value = v.TestResult;
            }
        }
    }
}
